﻿using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
//using Microsoft.Xna.Framework.GamerServices;
using YEngineLib;
using YEngineLib.Graphics;
using YEngineLib.Input;
using YEngineLib.State;
using YEngineLib.Util;

//using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
//using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
//using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using System.Threading;
using Spyfall.Objects;

using System.IO;

namespace Spyfall.States
{
    class ShopState : YBaseGameState
    {
        //States
        GamePadState currentGamePadState;
        GamePadState previousGameState;
        //Fonts
        SpriteFont spriteFont;
        SpriteFont spriteFontBIG;
        Texture2D upgradesBlank;
        Texture2D upgradeOptions;
        Texture2D buffBlank;
        Texture2D buffOptions;
        Texture2D cursorPic;
        Texture2D haveSlowPic;
        Texture2D haveCoinPic;
        Texture2D haveShieldPic;
        Texture2D movementLevel1;
        Texture2D movementLevel2;
        Texture2D movementLevel3;
        Texture2D movementLevel4;
        Texture2D movementLevel5;
        Texture2D speedLevel1;
        Texture2D speedLevel2;
        Texture2D speedLevel3;
        Texture2D DistanceLevel1;
        Texture2D DistanceLevel2;
        Texture2D DistanceLevel3;
        Texture2D skyBackground;
        Texture2D plane;
        //Which controller the Player is using
        public PlayerIndex playerIndex;
        //objectManager
        private Managers.ObjectManager objectManager;
        //screenPosition;
        Vector2 screenPosition;
        Rectangle wholescreen;
        Rectangle cursorRect;

        int cursorIndex;
        bool upgradeNotBuff;

        bool haveDoubleCoin;
        bool haveSlowMo;
        bool haveShield;

        int movementLevel;
        int dashSpeedLevel;
        int dashDistanceLevel;

        int playerCoins;

        //Sound Stuff ****************************************
        SoundEffect selected;
        SoundEffect selectButton;

        public ShopState()
        {
            objectManager = new Managers.ObjectManager();
            screenPosition = new Vector2(10.0f, 30.0f);
            wholescreen = new Rectangle(0, 0, 1280, 720);
            cursorRect = new Rectangle(0, 0, 1280, 720);
            cursorIndex = 1;
            upgradeNotBuff = true;
            haveDoubleCoin = false;
            haveSlowMo = false;
            haveShield = false;
            movementLevel = 0;
            dashSpeedLevel = 0;
            dashDistanceLevel = 0;
            playerCoins = 150;


        }

        public override void OnEnter(string previousState)
        {
            base.OnEnter(previousState);


#if XBOX
            if (Global.SaveDevice.FileExists(Global.containerName, Global.fileName_game))
            {
                Global.SaveDevice.Load(
                    Global.containerName,
                    Global.fileName_game,
                    stream =>
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            movementLevel = int.Parse(reader.ReadLine());
                            dashSpeedLevel = int.Parse(reader.ReadLine());
                            dashDistanceLevel = int.Parse(reader.ReadLine());
                            playerCoins = int.Parse(reader.ReadLine());
                            haveDoubleCoin = bool.Parse(reader.ReadLine());
                            haveSlowMo = bool.Parse(reader.ReadLine());
                            haveShield = bool.Parse(reader.ReadLine());
                            //frobnicate = bool.Parse(reader.ReadLine());
                            //elf = int.Parse(reader.ReadLine());
                        }
                    });
            }
#endif
        }


        public override void LoadContent()
        {

            spriteFont = YEngine.Game.Content.Load<SpriteFont>("Fonts/clock");
            spriteFontBIG = YEngine.Game.Content.Load<SpriteFont>("Fonts/clockbig");
            upgradesBlank = YEngine.Game.Content.Load<Texture2D>("Graphics/upgradesblank");
            cursorPic = YEngine.Game.Content.Load<Texture2D>("Graphics/shopcursor");
            upgradeOptions = YEngine.Game.Content.Load<Texture2D>("Graphics/upgradeoptions");
            buffBlank = YEngine.Game.Content.Load<Texture2D>("Graphics/buffblank");
            buffOptions = YEngine.Game.Content.Load<Texture2D>("Graphics/buffoptions");
            haveSlowPic = YEngine.Game.Content.Load<Texture2D>("Graphics/havesomo");
            haveCoinPic = YEngine.Game.Content.Load<Texture2D>("Graphics/havedoublecoin");
            haveShieldPic = YEngine.Game.Content.Load<Texture2D>("Graphics/haveshield");
            movementLevel1 = YEngine.Game.Content.Load<Texture2D>("Graphics/mevement1");
            movementLevel2 = YEngine.Game.Content.Load<Texture2D>("Graphics/mevement2");
            movementLevel3 = YEngine.Game.Content.Load<Texture2D>("Graphics/mevement3");
            movementLevel4 = YEngine.Game.Content.Load<Texture2D>("Graphics/mevement4");
            movementLevel5 = YEngine.Game.Content.Load<Texture2D>("Graphics/mevement5");
            speedLevel1 = YEngine.Game.Content.Load<Texture2D>("Graphics/speed1");
            speedLevel2 = YEngine.Game.Content.Load<Texture2D>("Graphics/speed2");
            speedLevel3 = YEngine.Game.Content.Load<Texture2D>("Graphics/speed3");
            DistanceLevel1 = YEngine.Game.Content.Load<Texture2D>("Graphics/distance1");
            DistanceLevel2 = YEngine.Game.Content.Load<Texture2D>("Graphics/distance2");
            DistanceLevel3 = YEngine.Game.Content.Load<Texture2D>("Graphics/distance3");
            skyBackground = YEngine.Game.Content.Load<Texture2D>("Graphics/mmsky");
            plane = YEngine.Game.Content.Load<Texture2D>("Graphics/mmplane");

            //Sound Stuff ****************************************
            selectButton = YEngine.Game.Content.Load<SoundEffect>("Sounds/SFX_SELECTHIGHLIGHT");
            selected = YEngine.Game.Content.Load<SoundEffect>("Sounds/SFX_BUTTONSELECT");
            LoadPercentage = 100.0f;
        }

        public override void Input()
        {
            previousGameState = currentGamePadState;
            currentGamePadState = GamePad.GetState(playerIndex);

            if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
            {
                YEngine.State.ChangeState("MenuState");
                return;
            }


            //Keyboard shortcut to return back to the mainmenu
            if (currentGamePadState.Buttons.B == ButtonState.Pressed)
            {
                playerCoins = 222;
                // make sure the device is ready
                if (Global.SaveDevice.IsReady)
                {
                    // save a file asynchronously. this will trigger IsBusy to return true
                    // for the duration of the save process.
                    Global.SaveDevice.SaveAsync(
                        Global.containerName,
                        Global.fileName_game,
                        stream =>
                        {
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                writer.WriteLine(movementLevel);
                                writer.WriteLine(dashSpeedLevel);
                                writer.WriteLine(dashDistanceLevel);
                                writer.WriteLine(playerCoins);
                                writer.WriteLine(haveDoubleCoin);
                                writer.WriteLine(haveSlowMo);
                                writer.WriteLine(haveShield);
                               // writer.WriteLine(frobnicate);
                                //writer.WriteLine(elf);
                            }
                        });
                }



                YEngine.State.ChangeState("MenuState");
                return;
            }


            if (YEngine.Input.Keyboard.KeyPressed(Keys.Z))
            {
                if (upgradeNotBuff == false)
                {
                    if (cursorIndex == 1 && haveDoubleCoin == false)
                        haveDoubleCoin = true;
                    else if (cursorIndex == 2 && haveShield == false)       //have shield
                        haveShield = true;
                    else if (cursorIndex == 3 && haveSlowMo == false)
                        haveSlowMo = true;
                }
                else if (upgradeNotBuff == true)
                {
                    if (cursorIndex == 1 && movementLevel < 5)
                        movementLevel++;
                    else if (cursorIndex == 2 && dashSpeedLevel < 3)
                        dashSpeedLevel++;
                    else if (cursorIndex == 3 && dashDistanceLevel < 3)
                        dashDistanceLevel++;
                }
            }



            if (YEngine.Input.Keyboard.KeyPressed(Keys.Down))
            {
                if (cursorIndex != 3)
                    selectButton.Play(0.8f, 0.0f, 0.0f);
                    cursorIndex++;

            }

            if (YEngine.Input.Keyboard.KeyPressed(Keys.Up))
            {
                if (cursorIndex != 1)
                    selectButton.Play(0.8f, 0.0f, 0.0f);
                    cursorIndex--;
            }

            if (YEngine.Input.Keyboard.KeyPressed(Keys.Right))
            {
                if (upgradeNotBuff == true)
                {
                    upgradeNotBuff = false;
                    cursorIndex = 1;
                }
            }

            if (YEngine.Input.Keyboard.KeyPressed(Keys.Left))
            {
                if (upgradeNotBuff == false)
                {
                    upgradeNotBuff = true;
                    cursorIndex = 1;
                }
            }

        }

        public override void Update()
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                YEngine.Game.Exit();
                objectManager.UpdateObjects((float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds);
            }

            if (cursorIndex == 1)
                cursorRect = new Rectangle(0, 0, 1280, 720);
            else if (cursorIndex == 2)
                cursorRect = new Rectangle(0, 120, 1280, 720);
            else
                cursorRect = new Rectangle(0, 220, 1280, 720);
        }

        public override void Draw()
        {
            YEngine.Graphics.SpriteBatch.Begin();

            YEngine.Graphics.SpriteBatch.Draw(skyBackground, wholescreen, Color.White);
            YEngine.Graphics.SpriteBatch.Draw(plane, wholescreen, Color.White);

            if (upgradeNotBuff)
                YEngine.Graphics.SpriteBatch.Draw(upgradesBlank, wholescreen, Color.White);
            else
                YEngine.Graphics.SpriteBatch.Draw(buffBlank, wholescreen, Color.White);
            YEngine.Graphics.SpriteBatch.Draw(cursorPic, cursorRect, Color.White);
            if (upgradeNotBuff)
            {
                YEngine.Graphics.SpriteBatch.Draw(upgradeOptions, wholescreen, Color.White);
                if(movementLevel == 1 )
                    YEngine.Graphics.SpriteBatch.Draw(movementLevel1, wholescreen, Color.White);
                else if (movementLevel == 2)
                    YEngine.Graphics.SpriteBatch.Draw(movementLevel2, wholescreen, Color.White);
                else if (movementLevel == 3)
                    YEngine.Graphics.SpriteBatch.Draw(movementLevel3, wholescreen, Color.White);
                else if (movementLevel == 4)
                    YEngine.Graphics.SpriteBatch.Draw(movementLevel4, wholescreen, Color.White);
                else if (movementLevel == 5)
                    YEngine.Graphics.SpriteBatch.Draw(movementLevel5, wholescreen, Color.White);

                if (dashSpeedLevel == 1)
                    YEngine.Graphics.SpriteBatch.Draw(speedLevel1, wholescreen, Color.White);
                else if (dashSpeedLevel == 2)
                    YEngine.Graphics.SpriteBatch.Draw(speedLevel2, wholescreen, Color.White);
                else if (dashSpeedLevel == 3)
                    YEngine.Graphics.SpriteBatch.Draw(speedLevel3, wholescreen, Color.White);

                if (dashDistanceLevel == 1)
                    YEngine.Graphics.SpriteBatch.Draw(DistanceLevel1, wholescreen, Color.White);
                if (dashDistanceLevel == 2)
                    YEngine.Graphics.SpriteBatch.Draw(DistanceLevel2, wholescreen, Color.White);
                if (dashDistanceLevel == 3)
                    YEngine.Graphics.SpriteBatch.Draw(DistanceLevel3, wholescreen, Color.White);
            }
            else
            {
                YEngine.Graphics.SpriteBatch.Draw(buffOptions, wholescreen, Color.White);
                if (haveShield)
                    YEngine.Graphics.SpriteBatch.Draw(haveShieldPic, wholescreen, Color.White);         //have shield
                if (haveDoubleCoin)
                    YEngine.Graphics.SpriteBatch.Draw(haveCoinPic, wholescreen, Color.White);
                if (haveSlowMo)
                    YEngine.Graphics.SpriteBatch.Draw(haveSlowPic, wholescreen, Color.White);
            }


            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, " " + playerCoins, new Vector2(1045, 125), Color.Black);


            if (upgradeNotBuff)
            {
                YEngine.Graphics.SpriteBatch.DrawString(spriteFontBIG, "5", new Vector2(840, 250), Color.Black);

                YEngine.Graphics.SpriteBatch.DrawString(spriteFontBIG, "3", new Vector2(840, 350), Color.Black);

                YEngine.Graphics.SpriteBatch.DrawString(spriteFontBIG, "4", new Vector2(840, 450), Color.Black);
            }

            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Shop State Prototype PRESS ENTER || B to return to Main Menu", screenPosition, Color.White);
            YEngine.Graphics.SpriteBatch.End();
        }

        public bool Sheild
        {
            get { return haveShield; }
            set { haveShield = value; }
        }

    }
}

